
package com.tools.cleanmaster.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.tools.cleanmaster.R;
import com.tools.cleanmaster.model.item.ImageTrashItem;
import com.tools.cleanmaster.utils.Utils;

import java.io.File;
import java.util.List;

/**
 * 照片详情
 */
public class TrashImageShowAdapter extends DeepAdapter {
    private List<ImageTrashItem> mDataList;
    private OnClickCallback mImageShowCallback;

    private int mImageSize;
    private int sScreenWidth;

    public TrashImageShowAdapter(Context context, List<ImageTrashItem> datas) {
        super(context);
        this.mDataList = datas;
        initImageSize();
    }

    @Override
    protected int getDefaultImageId() {
        return R.drawable.image_default;
    }

    private class Holder {
        private ImageView mIv;
        private ImageView mSelected;
        private ImageView mMask;
        private ImageView mSelectMask;
    }

    public interface OnClickCallback {
        void onClick(int position, String path);
    }

    public void setItemShowCallback(OnClickCallback itemshowCallback) {
        mImageShowCallback = itemshowCallback;
    }

    @Override
    public int getCount() {
        int count = 0;
        if (mDataList != null) {
            count = mDataList.size();
        }
        return count;
    }

    @Override
    public Object getItem(int position) {
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final Holder holder;
        if (convertView == null) {
            holder = new Holder();
            convertView = View.inflate(mContext, R.layout.item_image_grid, null);
            holder.mIv = convertView.findViewById(R.id.image);
            holder.mSelected = convertView.findViewById(R.id.isselected);
            holder.mMask = convertView.findViewById(R.id.mask);
            holder.mSelectMask = convertView.findViewById(R.id.selectmask);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.mIv.getLayoutParams();
        params.width = mImageSize;
        params.height = mImageSize;
        holder.mIv.setLayoutParams(params);
        holder.mMask.setLayoutParams(params);

        final ImageTrashItem item = mDataList.get(position);
        holder.mIv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(item.imagePath)) {
                    mImageShowCallback.onClick(position, item.imagePath);
                }
            }
        });

        Glide.with(mContext).load(new File(item.imagePath)).centerCrop().into(holder.mIv);

        setMask(holder, item);

        holder.mSelectMask.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                item.isSelected = !item.isSelected;
                if (mDeleteOnclickCallback != null) {
                    mDeleteOnclickCallback.onClick(position, item);
                }
                setMask(holder, item);
            }
        });
        return convertView;
    }

    private void setMask(Holder holder, ImageTrashItem item) {
        if (item.isSelected) {
            holder.mSelected.setImageResource(R.drawable.function_appsdetail_check);
            holder.mMask.setVisibility(View.VISIBLE);
        } else {
            holder.mSelected.setImageResource(R.drawable.function_appsdetail_uncheck);
            holder.mMask.setVisibility(View.INVISIBLE);
        }
    }

    private void initImageSize() {

        sScreenWidth = Utils.getScreenWidthPixels();

        mImageSize = sScreenWidth - mContext.getResources().getDimensionPixelOffset(R.dimen
                .trash_clean_image_grid_margin) * 2;
        mImageSize -=
                mContext.getResources().getDimensionPixelOffset(R.dimen.trash_clean_image_grid_horizontal_spacing) * 2;
        mImageSize /= 3;
    }

}
